package com.cisdi.info.simple.service.organization.impl;

import com.cisdi.info.simple.dao.organization.TestEmployeeDao;
import com.cisdi.info.simple.dto.base.PageDTO;
import com.cisdi.info.simple.dto.base.PageResultDTO;
import com.cisdi.info.simple.entity.base.BaseEntity;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import com.cisdi.info.simple.entity.organization.TestEmployee;
import com.cisdi.info.simple.service.base.BaseService;
import com.cisdi.info.simple.service.organization.TestEmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;
import java.util.Map;

import com.cisdi.info.simple.DDDException;

@Service
@Transactional
public class TestEmployeeServiceBean extends BaseService implements TestEmployeeService {

	private static Logger logger = LogManager.getLogger();

	@Autowired
	private TestEmployeeDao testEmployeeDao;

	public PageResultDTO findTestEmployees(PageDTO pageDTO)
	{
		pageDTO.setStartIndex((pageDTO.getCurrentPage()-1)*pageDTO.getPageSize());
		List<TestEmployee> testEmployeeDTOS = this.testEmployeeDao.findTestEmployees(pageDTO);
		Long totalCount = this.testEmployeeDao.findTestEmployeeTotalCount(pageDTO);

		PageResultDTO pageResultDTO = new PageResultDTO();
		pageResultDTO.setTotalCount(totalCount);
		pageResultDTO.setDatas(testEmployeeDTOS);

		return pageResultDTO;
	}

	public List<TestEmployee> findAllTestEmployees()
	{
		return this.testEmployeeDao.findAllTestEmployees();
	}

	public List<TestEmployee> findAllTestEmployeesWithIdName()
	{
		return this.testEmployeeDao.findAllTestEmployeesWithIdName();
	}

	public List<TestEmployee> findTestEmployeesWithIdNameByName(String testEmployeeName)
	{
		return this.testEmployeeDao.findTestEmployeesWithIdNameByName(testEmployeeName);
	}

	public TestEmployee findTestEmployeesWithIdNameById(Long testEmployeeId)
	{
		return this.testEmployeeDao.findTestEmployeesWithIdNameById(testEmployeeId);
	}

	public TestEmployee findTestEmployee(Long testEmployeeId)
	{
		return this.testEmployeeDao.findTestEmployee(testEmployeeId);
	}

	//所有外键的Name都以加载
	public TestEmployee findTestEmployeeWithForeignName(Long testEmployeeId)
	{
		return this.testEmployeeDao.findTestEmployeeWithForeignName(testEmployeeId);
	}

	public TestEmployee saveTestEmployee(TestEmployee testEmployee)
	{
		//TODO:请在此校验参数的合法性
		this.setSavePulicColumns(testEmployee);
		Integer rows = this.testEmployeeDao.saveTestEmployee(testEmployee);
		if(rows != 1)
		{
			String error = "新增保存测试表单出错，数据库应该返回1,但返回了 "+rows;
			throw new DDDException(error);
		}
		return testEmployee;
	}

	public TestEmployee updateTestEmployee(TestEmployee testEmployee)
	{
		//TODO:请在此校验参数的合法性
		this.setUpdatePulicColumns(testEmployee);
		Integer rows = this.testEmployeeDao.updateTestEmployee(testEmployee);
		if(rows != 1)
		{
			String error = "修改保存测试表单出错，数据库应该返回1,但返回了 "+rows+",数据可能被删除";
			throw new DDDException(error);
		}
		return testEmployee;
	}

	public void deleteTestEmployee(Long testEmployeeId)
	{
		Map<Class<? extends BaseEntity>,EntityUsage> entityUsageMap = this.checkForeignEntity(TestEmployee.class, testEmployeeId);
		if(entityUsageMap != null && entityUsageMap.size() >0)
		{
			StringBuilder errors = new StringBuilder();
			errors.append("要删除的数据正在被以下数引用\n");
			for(EntityUsage entityUsage : entityUsageMap.values())
			{
				errors.append("\t").append(entityUsage.getEntityLabel()).append("\n");
				for(Map.Entry<Long,String> entry : entityUsage.getUsageIdNames().entrySet() )
				{
					errors.append("\t\t").append(entry.getKey()).append("\t").append(entry.getValue()).append("\n");
				}
			}
			errors.append("，不能删除，请检查处理后再删除");
			throw  new DDDException(errors.toString());
		}

		Integer rows = this.testEmployeeDao.deleteTestEmployee(testEmployeeId);
		if(rows != 1)
		{
			String error = "删除测试表单出错，数据库应该返回1,但返回了 "+rows+",数据可能已经被删除";
			throw new DDDException(error);
		}
	}
}
